package features.advance.ch2;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;

public class xmlDemo {
    public static void main(String[] args) {
        try{
            parseXml("config.xml");

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void parseXml(String filename) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        File f = new File(filename);
        Document doc = builder.parse(f);
        Element root = doc.getDocumentElement();
        NodeList childNodes = root.getChildNodes();

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if(item instanceof Element){
                Element childElement = (Element) item;
                System.out.println(childElement);

            }
        }
        NodeList body = root.getElementsByTagName("font");
        NodeList itemList = body.item(0).getChildNodes();
        for (int i = 0; i < itemList.getLength(); i++) {
            Node item = itemList.item(i);
            if(item instanceof Element){
                Element childElement =(Element) item;
                Text textNode = (Text) childElement.getFirstChild();
                String text = textNode.getData().trim();
                System.out.println(text);
            }

        }
    }
}